package com.zhs.gccs.view.gt;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.model.CheckBoxModel;
import com.zhs.gccs.view.BaseView;

import java.util.ArrayList;
import java.util.List;

public class YearSelectView extends BaseView {
    private TextView tv_cancel;
    private TextView tv_confirm;
    private View ll_cb_all;
    private CheckBox cb_all_selected;
    private RecyclerView rv_items;
    private List<CheckBoxModel> nfList;
    private YearItemAdapter adapter;

    public YearSelectView(@NonNull Context context) {
        super(context);
    }

    public YearSelectView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public YearSelectView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_year,
                this, true);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_confirm = view.findViewById(R.id.tv_confirm);
        ll_cb_all = view.findViewById(R.id.ll_cb_all);
        cb_all_selected = view.findViewById(R.id.cb_all_selected);
        rv_items = view.findViewById(R.id.rv_items);
        tv_cancel.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Stream.of(nfList).forEach(item->item.setSelected(false));
                cb_all_selected.setChecked(false);
                adapter.notifyDataSetChanged();
//                popupWindow.dismiss();
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
        tv_confirm.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if (dataChangeListener != null) dataChangeListener.dismis();
            }
        });
        ll_cb_all.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                boolean checked = !cb_all_selected.isChecked();
                Stream.of(nfList).forEach(item->item.setSelected(checked));
                cb_all_selected.setChecked(checked);
                ll_cb_all.setTag(checked);
                adapter.notifyDataSetChanged();
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
        this.nfList = new ArrayList<>();
        rv_items.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        adapter = new YearItemAdapter(context, nfList);
//        rv_items.addItemDecoration(new DividerItemDecoration(context
//                , DividerItemDecoration.HORIZONTAL));
//        rv_items.addItemDecoration(new DividerItemDecoration(context
//                , DividerItemDecoration.VERTICAL));
        rv_items.setAdapter(adapter);
        adapter.setOperationListener(new YearItemAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                CheckBoxModel checkBoxModel = nfList.get(position);
                checkBoxModel.setSelected(!checkBoxModel.isSelected());
                adapter.notifyItemChanged(position);
                long count = Stream.of(nfList).filter(i -> i.isSelected()).count();
                if(count==nfList.size()){
                    cb_all_selected.setChecked(true);
                }else {
                    cb_all_selected.setChecked(false);
                }
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
    }

    public void initData(List<String> years) {
        if (years != null) {
            for (String y : years) {
                this.nfList.add(new CheckBoxModel(y, y, false));
            }
            adapter.notifyDataSetChanged();
        }
    }

    public void setDataChangeListener(DataChangeListener dataChangeListener) {
        this.dataChangeListener = dataChangeListener;
    }

    private DataChangeListener dataChangeListener;

    public List<CheckBoxModel> getSelected() {
        return Stream.of(nfList).filter(i -> i.isSelected()).toList();
    }

    public List<String> getSelectedValue() {
        return Stream.of(nfList).filter(i -> i.isSelected()).map(i -> i.getValue()).toList();
    }

    public interface DataChangeListener{
        void  dataChange();

        void dismis();
    }
}
